package ncku.account.gui;


import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

import ncku.account.data.GlobalData;
import ncku.account.event.Launcher;
import ncku.account.gui.toolBarAccount.ImportGUI;

public class newAccount extends JPanel {
   
    private JLabel newAccount;
    private JLabel name;
    private JLabel amount;
    private JLabel moneyAlert;
    private JToggleButton create;
    private JToggleButton cancel;
    private JTextField nameBox;
    private JTextField amountBox;
    private JTextField alertBox;
    private JCheckBox isAlert;
    
    
   public newAccount(){
  	//新增及初始化元件
    	newAccount = new JLabel ("新增帳戶");
        newAccount.setFont(new Font("SansSerif", Font.BOLD, 25));
        name = new JLabel ("帳戶名稱:");
        amount = new JLabel ("帳戶金錢:");
        moneyAlert = new JLabel("(餘額低於                   元後便啟動提醒)");
        isAlert = new JCheckBox("餘額提醒");
        //初始化按鈕及加上執行函式
        create = new JToggleButton ("確定", false);
        create.addActionListener(new NewAccountButton());
        cancel = new JToggleButton ("取消", false);
        cancel.addActionListener(new CancelNewAccountButton());
        nameBox = new JTextField (5);
        amountBox = new JTextField (5);
        alertBox = new JTextField(5);
        //setPreferredSize (new Dimension (681, 422));
        setLayout (null);        
      
        //加入元件到panel 裡面
        this.add (newAccount);
        this.add (name);
        this.add (amount);
        this.add (moneyAlert);
        this.add (create);
        this.add (cancel);
        this.add (nameBox);
        this.add (amountBox); 
        this.add (alertBox);
        this.add (isAlert);
        //自訂物件位置
        newAccount.setBounds (200, 50, 200, 25);
        name.setBounds (100, 135, 100, 25);
        amount.setBounds (100, 200, 100, 25);
        isAlert.setBounds(200,250,75,25);
        moneyAlert.setBounds (278,250,200,25);
        alertBox.setBounds(330,250,50,25);
        create.setBounds (200, 320, 100, 60);
        cancel.setBounds (300, 320, 100, 60);
        nameBox.setBounds (200, 125,200, 40);
        amountBox.setBounds (200, 195, 200, 40);   
    }

    class NewAccountButton implements ActionListener{
		//動作函式
		@Override
		public void actionPerformed(ActionEvent e) {
			if(nameBox.getText().length() !=0 && amountBox.getText().length() !=0 ){
				if(isAlert.isSelected()){  //有勾選餘額提醒
					try{
						int temp = Integer.valueOf(alertBox.getText());
						if(temp>=0){
							GlobalData.isAlert = true;
							GlobalData.alertMoney = temp;
							newAccount();
						}
						else{
							JOptionPane.showMessageDialog(Launcher.frame, "餘額提醒需輸入大於等於0的正整數", "提示訊息", JOptionPane.ERROR_MESSAGE);
						}
					}
					catch(Exception ex){
						JOptionPane.showMessageDialog(Launcher.frame, "餘額提醒需輸入大於等於0的正整數", "提示訊息", JOptionPane.ERROR_MESSAGE);
					}
				}	
				else{
					newAccount();
				}
			}
			//未輸入的錯誤處理
			else{
				JOptionPane.showMessageDialog(Launcher.frame, "匯入失敗！輸入不完全。", "提示訊息", JOptionPane.ERROR_MESSAGE);
			}
				
			
		}
	}

    class CancelNewAccountButton implements ActionListener{
		//動作函式
		@Override
		public void actionPerformed(ActionEvent e) {
			Launcher.frame.getContentPane().remove(1);
			Launcher.frame.getContentPane().add (BorderLayout.CENTER,new mainPage());
			Launcher.frame.getContentPane().validate();		
		}
	}
    
    void newAccount(){
    	//更新帳戶名稱以及帳戶現有金錢
		GlobalData.accountName = nameBox.getText();
		GlobalData.accountMoney = Integer.valueOf(amountBox.getText());
		GlobalData.balanceMoney =  Integer.valueOf(amountBox.getText());
		//切換panel ，先把原本的移除掉，然後加入新的
		Launcher.frame.getContentPane().remove(1);
		Launcher.frame.getContentPane().add (BorderLayout.CENTER,new newTransaction());
		Launcher.frame.getContentPane().doLayout();
    }
}
